package com.gxa.insurance.controller;

import com.gxa.insurance.pojo.S4Shop;
import com.gxa.insurance.service.S4ShopService;
import com.gxa.insurance.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author wang li sha
 * @date 2022-06-06 17:58
 */
@Api(value = "4S店s4Shop相关的api接口")
@RestController
@RequestMapping("/s4Shop")
@CrossOrigin
public class S4ShopController {
    @Autowired
    S4ShopService s4ShopService;

    @ApiOperation("添加4S店的方法")
    @PostMapping("/insert")
    public Map insert(@RequestBody S4Shop s4Shop) throws Exception {
        Integer insert = s4ShopService.insert(s4Shop);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(insert);
        return jsonResult.getMap();
    }

    @ApiOperation("修改4S店信息的方法")
    @PostMapping("/update")
    public Map update(@RequestBody S4Shop s4Shop) throws Exception {
        Integer update = s4ShopService.update(s4Shop);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(update);
        return jsonResult.getMap();
    }

    @ApiOperation("根据id删除4S店的方法")
    @PostMapping("/deleteById")
    public Map deleteById(@RequestParam("id") Integer id) throws Exception {
        Integer delete = s4ShopService.deleteById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(delete);
        return jsonResult.getMap();
    }

    @ApiOperation("根据id查询4S店的方法")
    @PostMapping("/selectById")
    public Map selectById(@RequestParam("id") Integer id) throws Exception {
        S4Shop s4Shop = s4ShopService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(s4Shop);
        return jsonResult.getMap();
    }

    @ApiOperation("分页查询所有4S店的方法")
    @PostMapping("/selectAllByPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页记录数", required = true, dataType = "int", paramType = "query", defaultValue = "5"),
    })
    public Map selectAllByPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size) throws Exception {
        List<S4Shop> s4Shops = s4ShopService.selectAllByPage(page, size);
        Integer count = s4ShopService.selectAll().size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(s4Shops);
        jsonResult.put("count", count);
        return jsonResult.getMap();
    }


    @ApiOperation("分页模糊查询所有4S店的方法")
    @PostMapping("/selectLikeNameByPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页记录数", required = true, dataType = "int", paramType = "query", defaultValue = "5"),
            @ApiImplicitParam(name = "name", value = "4s店名", required = true, dataType = "string", paramType = "query", defaultValue = "成都"),
    })
    public Map selectLikeNameByPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestParam("name") String name) throws Exception {
        List<S4Shop> s4Shops = s4ShopService.selectLikeNameByPage(page, size, name);
        Integer count = s4ShopService.selectLikeName(name).size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(s4Shops);
        jsonResult.put("count", count);
        return jsonResult.getMap();
    }
}
